package slidingwindow;

import java.util.Arrays;

public class Heaters475 {
    public int findRadius(int[] houses, int[] heaters) {
        int max = 0;
        Arrays.sort(heaters);
        for (int house : houses) {
            int dis = this.getMinDis(house, heaters, 0, heaters.length - 1);
            max = max > dis ? max : dis;
        }
        return max;
    }

    public int getMinDis(int a, int[] heaters, int start, int end) {
        int mid = (start + end) / 2;
        if (start == mid) {
            return min(Math.abs(a - heaters[mid]), Math.abs(a - heaters[end]));
        }
        if (heaters[mid] == a) {
            return 0;
        }
        if (heaters[mid] > a) {
            return getMinDis(a, heaters, start, mid);
        } else {
            return getMinDis(a, heaters, mid, end);
        }
    }

    public int min(int a, int b) {
        return a < b ? a : b;
    }
}
